package com.atguigu.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Date:2020/9/14
 * Author:ybc
 * Description:
 */
public class CookieServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建cookie
        Cookie cookie1 = new Cookie("username", "admin");
        Cookie cookie2 = new Cookie("password", "123");
        Cookie cookie3 = new Cookie("username", "root");
        //设置cookie的有效时间
        //cookie1.setMaxAge(-100);
        //设置cookie的有效路径
        cookie1.setPath(request.getContextPath()+"/success.html");
        //将cookie发送到浏览器中
        response.addCookie(cookie1);
        response.addCookie(cookie2);
        //response.addCookie(cookie3);
        //转发到success.html
        request.getRequestDispatcher("/success.html").forward(request, response);
    }
}
